﻿<html><body style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>private</span> <span style='color:#800000; font-weight:bold; '>void</span> createPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create a PDF document</span>
    Document pdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    pdfDocument<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Display the attachments panel when the PDF document is opened in a PDF viewer</span>
    pdfDocument<span style='color:#808030; '>.</span>ViewerPreferences<span style='color:#808030; '>.</span>PageMode <span style='color:#808030; '>=</span> ViewerPageMode<span style='color:#808030; '>.</span>UseAttachments<span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Add a page to PDF document</span>
    PdfPage pdfPage <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>AddPage<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    Cursor <span style='color:#808030; '>=</span> Cursors<span style='color:#808030; '>.</span>WaitCursor<span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>string</span> outPdfFile <span style='color:#808030; '>=</span> @<span style='color:#800000; '>"</span><span style='color:#0000e6; '>DemoAppFiles\Output\PDF_Creator\File_Attachments.pdf</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// The titles font used to mark various sections of the PDF document</span>
        PdfFont titleFont <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>AddFont<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>new</span> Font<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Times New Roman</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>10</span><span style='color:#808030; '>,</span> FontStyle<span style='color:#808030; '>.</span>Bold<span style='color:#808030; '>,</span> GraphicsUnit<span style='color:#808030; '>.</span>Point<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        PdfFont subtitleFont <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>AddFont<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>new</span> Font<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Times New Roman</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>8</span><span style='color:#808030; '>,</span> FontStyle<span style='color:#808030; '>.</span>Regular<span style='color:#808030; '>,</span> GraphicsUnit<span style='color:#808030; '>.</span>Point<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#800000; font-weight:bold; '>float</span> xLocation <span style='color:#808030; '>=</span> <span style='color:#008c00; '>5</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>float</span> yLocation <span style='color:#808030; '>=</span> <span style='color:#008c00; '>5</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Add document title</span>
        TextElement titleTextElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> TextElement<span style='color:#808030; '>(</span>xLocation<span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attach Files and Streams to a PDF Document</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> titleFont<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        AddElementResult addElementResult <span style='color:#808030; '>=</span> pdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>titleTextElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        yLocation <span style='color:#808030; '>=</span> addElementResult<span style='color:#808030; '>.</span>EndPageBounds<span style='color:#808030; '>.</span>Bottom <span style='color:#808030; '>+</span> <span style='color:#008c00; '>15</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from a file without icon</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileAttachmentPath <span style='color:#808030; '>=</span> @<span style='color:#800000; '>"</span><span style='color:#0000e6; '>DemoAppFiles\Input\Attach_Files\Attachment_File.txt</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        pdfDocument<span style='color:#808030; '>.</span>AddFileAttachment<span style='color:#808030; '>(</span>fileAttachmentPath<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from File</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from a stream without icon</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileStreamAttachmentPath <span style='color:#808030; '>=</span> @<span style='color:#800000; '>"</span><span style='color:#0000e6; '>DemoAppFiles\Input\Attach_Files\Attachment_Stream.txt</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream attachmentStream <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream<span style='color:#808030; '>(</span>fileStreamAttachmentPath<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileMode<span style='color:#808030; '>.</span>Open<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileAccess<span style='color:#808030; '>.</span>Read<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        pdfDocument<span style='color:#808030; '>.</span>AddFileAttachment<span style='color:#808030; '>(</span>attachmentStream<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment_Stream.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from Stream</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Add the text element</span>
        <span style='color:#800000; font-weight:bold; '>string</span> text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Click the next icon to open the attachment from a file:</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>float</span> textWidth <span style='color:#808030; '>=</span> subtitleFont<span style='color:#808030; '>.</span>GetTextWidth<span style='color:#808030; '>(</span>text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        TextElement textElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> TextElement<span style='color:#808030; '>(</span>xLocation<span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> text<span style='color:#808030; '>,</span> subtitleFont<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        addElementResult <span style='color:#808030; '>=</span> pdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>textElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from file with paperclip icon in PDF</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileAttachmentWithIconPath <span style='color:#808030; '>=</span> @<span style='color:#800000; '>"</span><span style='color:#0000e6; '>DemoAppFiles\Input\Attach_Files\Attachment_File_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        <span style='color:#696969; '>// Create the attachment from file</span>
        RectangleF attachFromFileIconRectangle <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> RectangleF<span style='color:#808030; '>(</span>xLocation <span style='color:#808030; '>+</span> textWidth <span style='color:#808030; '>+</span> <span style='color:#008c00; '>3</span><span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> <span style='color:#008c00; '>6</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>10</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        FileAttachmentElement attachFromFileElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> FileAttachmentElement<span style='color:#808030; '>(</span>attachFromFileIconRectangle<span style='color:#808030; '>,</span> fileAttachmentWithIconPath<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        attachFromFileElement<span style='color:#808030; '>.</span>IconType <span style='color:#808030; '>=</span> FileAttachmentIcon<span style='color:#808030; '>.</span>Paperclip<span style='color:#800080; '>;</span>
        attachFromFileElement<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from File with Paperclip Icon</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        attachFromFileElement<span style='color:#808030; '>.</span>IconColor <span style='color:#808030; '>=</span> Color<span style='color:#808030; '>.</span>Blue<span style='color:#800080; '>;</span>
        pdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>attachFromFileElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        yLocation <span style='color:#808030; '>=</span> addElementResult<span style='color:#808030; '>.</span>EndPageBounds<span style='color:#808030; '>.</span>Bottom <span style='color:#808030; '>+</span> <span style='color:#008c00; '>10</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Add the text element</span>
        text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Click the next icon to open the attachment from a stream:</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        textWidth <span style='color:#808030; '>=</span> subtitleFont<span style='color:#808030; '>.</span>GetTextWidth<span style='color:#808030; '>(</span>text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        textElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> TextElement<span style='color:#808030; '>(</span>xLocation<span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> text<span style='color:#808030; '>,</span> subtitleFont<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        addElementResult <span style='color:#808030; '>=</span> pdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>textElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create an attachment from stream with pushpin icon in PDF</span>
        <span style='color:#800000; font-weight:bold; '>string</span> fileStreamAttachmentWithIconPath <span style='color:#808030; '>=</span> @<span style='color:#800000; '>"</span><span style='color:#0000e6; '>DemoAppFiles\Input\Attach_Files\Attachment_Stream_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream attachmentStreamWithIcon <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileStream<span style='color:#808030; '>(</span>fileStreamAttachmentWithIconPath<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileMode<span style='color:#808030; '>.</span>Open<span style='color:#808030; '>,</span> System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>FileAccess<span style='color:#808030; '>.</span>Read<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#696969; '>// Create the attachment from stream</span>
        RectangleF attachFromStreamIconRectangle <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> RectangleF<span style='color:#808030; '>(</span>xLocation <span style='color:#808030; '>+</span> textWidth <span style='color:#808030; '>+</span> <span style='color:#008c00; '>3</span><span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> <span style='color:#008c00; '>6</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>10</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        FileAttachmentElement attachFromStreamElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> FileAttachmentElement<span style='color:#808030; '>(</span>attachFromStreamIconRectangle<span style='color:#808030; '>,</span> attachmentStreamWithIcon<span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment_Stream_Icon.txt</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        attachFromStreamElement<span style='color:#808030; '>.</span>IconType <span style='color:#808030; '>=</span> FileAttachmentIcon<span style='color:#808030; '>.</span>PushPin<span style='color:#800080; '>;</span>
        attachFromStreamElement<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Attachment from Stream with Pushpin Icon</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        attachFromStreamElement<span style='color:#808030; '>.</span>IconColor <span style='color:#808030; '>=</span> Color<span style='color:#808030; '>.</span>Green<span style='color:#800080; '>;</span>
        pdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>attachFromStreamElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Save the PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Write the memory buffer in a PDF file</span>
        System<span style='color:#808030; '>.</span>IO<span style='color:#808030; '>.</span>File<span style='color:#808030; '>.</span>WriteAllBytes<span style='color:#808030; '>(</span>outPdfFile<span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>catch</span> <span style='color:#808030; '>(</span>Exception ex<span style='color:#808030; '>)</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// The PDF creation failed</span>
        MessageBox<span style='color:#808030; '>.</span>Show<span style='color:#808030; '>(</span>String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Create PDF Document Error. {0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> ex<span style='color:#808030; '>.</span>Message<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>return</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>finally</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Close the PDF document</span>
        pdfDocument<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        Cursor <span style='color:#808030; '>=</span> Cursors<span style='color:#808030; '>.</span>Arrow<span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>

    <span style='color:#696969; '>// Open the created PDF document in default PDF viewer</span>
    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        System<span style='color:#808030; '>.</span>Diagnostics<span style='color:#808030; '>.</span>Process<span style='color:#808030; '>.</span>Start<span style='color:#808030; '>(</span>outPdfFile<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>catch</span> <span style='color:#808030; '>(</span>Exception ex<span style='color:#808030; '>)</span>
    <span style='color:#800080; '>{</span>
        MessageBox<span style='color:#808030; '>.</span>Show<span style='color:#808030; '>(</span>String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Cannot open created PDF file '{0}'. {1}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfFile<span style='color:#808030; '>,</span> ex<span style='color:#808030; '>.</span>Message<span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</body>
</html>